﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DVBViMon.Utils;
using iMon.DisplayApi;

namespace DVBViMon
{
    partial class FormMain
    {
        private string lastLCDDisplayString = string.Empty;
        private int lastAudioMode = 0;
        private bool lastRecordingState = false;
        private int lastProgress = -1;

        string lastVFDdisplayStringLine1 = string.Empty;
        string lastVFDdisplayStringLine2 = string.Empty;

        /// <summary>
        /// Resets the initialization data.
        /// </summary>
        private void resetInitData()
        {
            lastVFDdisplayStringLine1 = string.Empty;
            lastVFDdisplayStringLine2 = string.Empty;

            lastLCDDisplayString = string.Empty;
            lastAudioMode = 0;
            lastRecordingState = false;
            lastProgress = -1;
        }

        /// <summary>
        /// Shows the DVBV values for LCD and VFD.
        /// </summary>
        /// <param name="iHt">The i ht.</param>
        public void ShowDvbvValuesLcdVfd(Dictionary<string, object> iHt)
        {
            if (this.displayType == iMonNativeApi.iMonDisplayType.LCD)
                ShowDvbvValuesLCD(iHt);
            else
                ShowDvbvValuesVFD(iHt);
        }

        /// <summary>
        /// Shows the DVBV values for VFD.
        /// </summary>
        /// <param name="iHt">The iHt dictionary data.</param>
        private void ShowDvbvValuesVFD(Dictionary<string, object> iHt)
        {
            string VFDdisplayStringLine1 = string.Empty;
            string VFDdisplayStringLine2 = string.Empty;

            bool OSDActive = (bool)iHt["isOSD"];
            bool OSDItemEmpty = iHt["selectedItem"].ToString() == string.Empty;

            if (OSDActive && !OSDItemEmpty)
            {
                VFDdisplayStringLine1 = iHt["selectedItem"].ToString();
            }
            else
            {
                if ((bool)iHt["isMediaPlayback"] && !(bool)iHt["isTimeshift"])
                {
                    if ((bool)iHt["isDVD"])
                        VFDdisplayStringLine1 = iHt["activeChannel"].ToString();
                    else
                        VFDdisplayStringLine1 = iHt["mediaTitle"].ToString();

                    if (this.durationRemainingFlag)
                        VFDdisplayStringLine2 = "Rem. " + iHt["remain"].ToString().Substring(0, 5);
                    else
                        VFDdisplayStringLine2 = "Dur. " + iHt["duration"].ToString().Substring(0, 5);
                }
                else
                {
                    VFDdisplayStringLine1 = iHt["activeChannel"].ToString();
                    VFDdisplayStringLine2 = iHt["title"].ToString();
                }
            }

            if (VFDdisplayStringLine1 != lastVFDdisplayStringLine1 || VFDdisplayStringLine2 != lastVFDdisplayStringLine2)
            {
                this.scrollTextTimerVFD.SetVfdText(VFDdisplayStringLine1, VFDdisplayStringLine2);
            }

            lastVFDdisplayStringLine1 = VFDdisplayStringLine1;
            lastVFDdisplayStringLine2 = VFDdisplayStringLine2;
        }

        /// <summary>
        /// Shows the DVBV values for LCD.
        /// </summary>
        /// <param name="iHt">The iHt dictionary data.</param>
        private void ShowDvbvValuesLCD(Dictionary<string, object> iHt)
        {

            string LCDdisplayString = string.Empty;
            string activeChannel = string.Empty;
            string title = string.Empty;
            int progress = 0;
            bool isRecording = false;
            int audioMode = 0;

            activeChannel = iHt["activeChannel"].ToString();

            string sProgress;
            if ((bool) iHt["isMediaPlayback"])
            {
                sProgress = iHt["percentage"].ToString();
            }
            else
            {
                sProgress = iHt["progress"].ToString();
            }

            try
            {
                progress = Convert.ToInt32(sProgress);
            }
            catch (Exception)
            {
                progress = 0;
            }

            if (progress != lastProgress)
            {
                try
                {
                    iMonNativeApi.IMON_Display_SetLcdProgress(progress, 100);
                }
                catch
                {
                }
                lastProgress = progress;
            }

            isRecording = (bool) iHt["isRecording"];
            if (isRecording != lastRecordingState)
            {
                try
                {
                    iMonNativeApi.IMON_Display_SetLcdEtcIcon(
                        (byte) (isRecording ? iMonNativeApi.OtherLcdIcons.Recording : 0));
                }
                catch
                {
                }
                lastRecordingState = isRecording;
            }


            audioMode = (int) iHt["audioMode"];


            /* 0=Mono 1=Stereo 2=DD2.0 3=DD5.1 */
            if (audioMode != lastAudioMode)
            {
                int speakers;
                try
                {
                    switch (audioMode)
                    {
                        case 0:
                            iMonNativeApi.IMON_Display_SetLcdSpeakerIcon(0, 0);
                            lastAudioMode = 0;
                            break;
                        case 1:
                            speakers = (int) iMonNativeApi.SoundLcdIcons.SpeakerFrontLeft |
                                       (int) iMonNativeApi.SoundLcdIcons.SpeakerFrontRight;
                            iMonNativeApi.IMON_Display_SetLcdSpeakerIcon((byte) (speakers & 0xff),
                                                                         (byte) ((speakers >> 8) & 0xff));
                            lastAudioMode = 1;
                            break;
                        case 2:
                            speakers = (int) iMonNativeApi.SoundLcdIcons.SpeakerFrontLeft |
                                       (int) iMonNativeApi.SoundLcdIcons.SpeakerFrontRight;
                            iMonNativeApi.IMON_Display_SetLcdSpeakerIcon((byte) (speakers & 0xff),
                                                                         (byte) ((speakers >> 8) & 0xff));
                            lastAudioMode = 2;
                            break;
                        case 3:
                            speakers = (int) iMonNativeApi.SoundLcdIcons.SpeakerFrontLeft |
                                       (int) iMonNativeApi.SoundLcdIcons.SpeakerFrontRight |
                                       (int) iMonNativeApi.SoundLcdIcons.SpeakerCenter |
                                       (int) iMonNativeApi.SoundLcdIcons.SpeakerRearLeft |
                                       (int) iMonNativeApi.SoundLcdIcons.SpeakerRearRight |
                                       (int) iMonNativeApi.SoundLcdIcons.SpeakerLFE;
                            iMonNativeApi.IMON_Display_SetLcdSpeakerIcon((byte) (speakers & 0xff),
                                                                         (byte) ((speakers >> 8) & 0xff));
                            lastAudioMode = 3;
                            break;

                        default:
                            iMonNativeApi.IMON_Display_SetLcdSpeakerIcon(0, 0);
                            lastAudioMode = audioMode;
                            break;
                    }
                }
                catch
                {
                }
            }

            bool OSDActive = (bool) iHt["isOSD"];
            bool OSDItemEmpty = iHt["selectedItem"].ToString() == string.Empty;

            if (OSDActive && !OSDItemEmpty)
            {
                LCDdisplayString = iHt["selectedItem"].ToString();
            }
            else
            {
                if (activeChannel != string.Empty)
                {
                    LCDdisplayString += activeChannel;
                }

                if ((bool) iHt["isMediaPlayback"] && !(bool) iHt["isTimeshift"])
                {
                    if ((bool) iHt["isDVD"])
                        LCDdisplayString = iHt["activeChannel"].ToString();
                    else
                        LCDdisplayString = iHt["mediaTitle"].ToString();
                }
                else
                {
                    title = iHt["title"].ToString();
                    if (title != string.Empty)
                    {
                        if (LCDdisplayString != string.Empty)
                            LCDdisplayString += " - " + title;
                        else
                            LCDdisplayString += title;
                    }
                }
            }


            if (LCDdisplayString != lastLCDDisplayString)
            {
                try
                {
                    if (LCDdisplayString == string.Empty)
                        this.scrollTextTimerLCD.SetLcdText(" ");
                    else
                        this.scrollTextTimerLCD.SetLcdText(LCDdisplayString.Substring(0,
                                                                                      LCDdisplayString.Length <
                                                                                      256
                                                                                          ? LCDdisplayString.
                                                                                                Length
                                                                                          : 256));
                }
                catch (Exception)
                {
                }
            }

            lastLCDDisplayString = LCDdisplayString;
        }
    }
}
